knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(EMigD)
library(lubridate)
library(dplyr)
library(tibble)

Setting Up the Data

Formatting the Data

Before moving into the data, we could first format the data so that we can go through the workflow smoothly using EmigD_format. This allows us to have a consistent column names as we move through the workflow. The arguments in EmigD_format consist of the necessary columns needed.

deer <- EmigD_format(deer, id_ = "ID", dt_ = "date", x_ = "utm_x", y = "utm_y",
                   year_ = "year", month_ = "month", jdate_ = "julian")

Create Intervals

We created 10-day intervals for deer in the January and July. We first created a start column using the floor_Date() function to obtain the 10-day extents within the months. We included the 31st day (if a month had 31 days) in the third 10-day interval to prevent the creation of a 4th 10-day interval with only a single day in it.

Intervals for January

jan <- deer %>%
  # Creates a start column assigning the first day in the 10-day interval in which
  # the date falls under (e.g., 01-03-2021 would be in the first 10-day interval
  # so the `floor_date` assigned to it would be 01-01-2021)
  mutate(start = floor_date(dt_, "10 days")) %>%
  # For any months that has 31 days, the 31st day would normally be assigned its 
  # own interval. The code below takes the 31st day and joins it with the 
  # previous interval. 
  mutate(start = if_else(day(start) == 31, start - days(10), start)) %>% 
  group_by(id_, start) %>%
  filter(month_ == "1") %>% 
  group_split()

Let's remove some list elements from the jan interval so that we have different number of intervals between jan and july.

jan <- jan[-c(17,18)]

Intervals for July

july <- deer %>%
  # Creates a start column assigning the first day in the 10-day interval in which
  # the date falls under (e.g., 01-03-2021 would be in the first 10-day interval
  # so the `floor_date` assigned to it would be 01-01-2021)
  mutate(start = floor_date(dt_, "10 days")) %>%
  # For any months that has 31 days, the 31st day would normally be assigned its 
  # own interval. The code below takes the 31st day and joins it with the 
  # previous interval. 
  mutate(start = if_else(day(start) == 31, start - days(10), start)) %>% 
  group_by(id_, start) %>%
  filter(month_ == "7") %>% 
  group_split()

Matching Intervals

Before we can begin matching the intervals, we first assign attributes to the two list (jan and july). We name this attribute match, and within this attribute table, we have two columns (id and interval_). We can use check to conveniently look our attribute table.

# Add an attribute to hold the attributes of each list element
attr(jan, "match") <- data.frame(id = sapply(jan, function(x) paste(x$id_[1])),
                                  interval_ = sapply(jan, function(x) paste(x$start[1]))
)

# Check the attributes
check(jan, "match")

# Add an attribute "tab" to hold the attributes of each list element
attr(july, "match") <- data.frame(id = sapply(july, function(x) paste(x$id_[1])),
                                  interval_ = sapply(july, function(x) paste(x$start[1]))
) 

# Check the attributes
check(july, "match")

Using the match_intervals function we can match the number of intervals in july to those in jan. The attribute_name is the name of the attribute that was added. We then include the two list that we want to match. The id_col and the interval_col are the two columns created for the added attribute.

match_list <- match_intervals(attribute_name = "match", list1 = july, list2 = jan, id_col = "id", 
                       interval_col = "interval_")

The output for match_ouput will be a list that contains two new lists. These two new list are the matching lists for the two specified in the arguments. These can be re-assigne to allow for readability or it can be used as is.

july <- match_list[[1]]
jan <- match_list[[2]]


jhnhng/EMigD documentation built on Oct. 3, 2023, 7:59 p.m.